-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(urlMatcherFactory): Decode slashes in string routes #2071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Slashes in string routes now get decoded (they were only encoded before). Related test was fixed.
@@ -578,7 +579,7 @@ function $UrlMatcherFactory() { | |||
decode: valFromString, | |||
// TODO: in 1.0, make string .is() return false if value is undefined/null by default. | |||
// In 0.2.x, string params are optional by default for backwards compat | |||
is: function(val) { return val == null || !isDefined(val) || typeof val === "string"; }, | |||
is: function(val) { return (val == null || !isDefined(val) || typeof val === "string") && (val.toString().indexOf(slashReplacement) === -1); }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This added condition fails when val
is null. I tested it in my project and came out with the following improvement:
is: function(val) { return val == null || !isDefined(val) || (isString(val) && val.indexOf(slashReplacement) === -1); },
What do you think @chliebel?
@piotrd Looks good! I updated the PR. |
@christopherthielen Are you taking care of this? 😉 |
@chliebel thanks for this fix! I've took the liberty to merge it into my fork and use it until the upstream gets updated. |
@cssensei You’re welcome! My pleasure. 😃 |
I've downloaded the latest version (0.2.15) and still notice that forward-slashes get double encoded For instance, |
@ehorodyski Please note that this fix is scheduled for 0.2.16 and not part of 0.2.15. However, you can fork the ui-router repository and merge this fix branch into it. |
is there a way to decorate the "$UrlMatcherFactory" with its fixed version? |
@chliebel In the description of the pull request you say that “ |
@chliebel thanks for the PR! I've done more analysis on the slash encoding. Because the browsers are at liberty to consider a url-encoded string to be the same as a non-encoded string (or not), we can not url-encode the slashes and get 100% bidirectional encode/decoding. I've switched to using tilde as an escape character for encoding slashes. This can be seen here: 02e9866 |
Sounds fair. 👍 |
this is very surprising to me as it defeats the purpose of url-encoding. in actuality, which browsers do this? |
@bblack you're correct, sorry. My memory conflated two separate things.
In fact, the location service is the cause of all this trouble: http://plnkr.co/edit/YziNoXrk2DiPQ20k8DeT?p=preview |
nice, that jives with what i've seen. i was going to try to dig into it tomorrow; now i don't have to. thanks! do you know if this is deliberate on their part, or if there's an open github issue for this difficulty with having slashes in route/query params? it would be nice if it were fixed in angular so we don't have to do workarounds like this. separate question: is there a way i can get ui-router's ~2F encoding method without having to implement it myself, in case it changes in the future? |
@bblack now that I have a clearer picture of what's happening, I'd be open to going all the way back to standard uriEncoding. I think we just need to encode the whole URL once before passing to location.url(), and also encode each string-type parameter once using encodeURIComponent. Are you willing to look at doing that? Otherwise I have too much in my plate to tackle it soon. |
You can get ui routers implementation from urlmatcher factory's |
i can try to take a look sometime later this week (no promises). |
let me know if you do work on it; I can offer some additional guidance |
Since it looks like you're actively working on this, I have a problem since upgrading ui-router to 0.2.17 in an app which is using ui-router's HTML5 mode, which I believe is directly related to this. In the main path part of the URL, (not the string route) slashes are getting encoded (with ~2F). Is this because the code is incorrectly encoding URLs parsed in HTML5 mode? Since upgrading ui-router the URL after a state.go() is rewritten in the browser as this: I have tested other apps after upgrading ui-router not using HTML5 mode and the URLs are written (as expected) without the ~2F encoding. I tried all versions of ui-router back to 0.2.12, and all of them somehow encode some (but not always all) the slashes in the main part of the URL with either (%2F, %252F or ~2F). I am currently stuck with v 0.2.11 because of this issue. |
@Red-3 it's expected since 0.2.12 that slashes in parameters are encoded. given these states:
if this: So, in the You can register a custom type that does not encode/decode, then use it in your parameters:
|
Thanks for your explanation @christopherthielen I now see where the problem is in my ui-router config. |
@Red-3 can I see what you ended up with? And also what you started with (the regex) |
@christopherthielen : In this situation, we send a URL out to potential customers via email and want Angular to read the token from it. It's a standard URL format (didn't want to add
So on the path:
(If our landing page was always called If you have a better suggestion on how to handle this scenario, I'd love to hear it! |
I think my previous comment should work for you, registering a named type that does no special encoding for slashes:
|
Good question! |
related issue in angular: angular/angular.js#13837 |
Slashes in string routes now get decoded (they were only encoded before). Related test was fixed.
The current behavior is the following: Encoding the state parameter
hello/world
correctly gets encoded tohello%252Fworld
, but reading it from the target controller returnshello%2Fworld
. This is because the value is recognized as a valid string and therefore isn’t decoded. Fixing theis
function of the string type now triggers the decode of slashes.In addition, the related tests was fixed. The description says that encoding and decoding is tested, however only encoding tests take place.